package components.tools
{
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_close;
	import UIFree.controls.uif_leavesBlock;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import classes.clt_events;
	
	import components.clt_arrowB;
	import components.clt_component;
	import components.clt_workspace;
	import components.tools.correction.clt_correctionPanel;
	import components.tools.photos.clt_photosLayer;
	
	import data.cd_background;
	import data.cd_masks;
	
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class clt_toolsBlock extends uif_leavesBlock {
		private var _cutoutsLayer	: clt_selectLayer;
		private var _backgroundLayer: clt_selectLayer;
		private var _photosLayer	: clt_photosLayer;
		private var _corrLayer		: clt_correctionPanel;
		private var _toolLayer		: clt_component;
		private var _close			: uif_close;
		
		public static const HEADHEIGHT: Number = 30;
		public function clt_toolsBlock(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0) {
			super(parent, ax, ay, awidth, aheight, 'tools');
			var rect: Rectangle = block.getInner();
			_toolLayer = new clt_component(block, rect.x, rect.y, rect.width, rect.height);
			createCutouts();
			createPhotos();
			createBackgrounds();
			createCorr();
			
			addEvent(clt_events.SHOWTOOLS, doShowTools, workspace);
			rect = block.getInner();
			_close = new uif_close(block, rect.x + rect.width - 18, rect.y);
			block.addEvent(Event.CLOSE, doCanClose);
/*			var button: uif_arrowButtonFlat = new uif_arrowButtonFlat(block, rect.x + rect.width - HEADHEIGHT, rect.y, HEADHEIGHT, HEADHEIGHT, 'bflat')
			button.orienation = 1;
			button.addEvent(MouseEvent.CLICK, doCanClose);*/
		}
		
		protected function createPhotos(): void {
			var rect: Rectangle = _toolLayer.getInner();
			_photosLayer = new clt_photosLayer(_toolLayer, rect.x, rect.y, rect.width - HEADHEIGHT, rect.height);
			_photosLayer.visible = false;
		}
		
		protected function createBackgrounds(): void {
			var rect: Rectangle = _toolLayer.getInner();
			_backgroundLayer = new clt_selectLayer(_toolLayer, cd_background, clt_servers.BACKGROUND_URL + 'preview/', clt_events.SELECTBACKGROUND, rect.x, rect.y, rect.width - HEADHEIGHT, rect.height);
			_backgroundLayer.visible = false;
		}
		
		protected function createCutouts(): void {
			var rect: Rectangle = _toolLayer.getInner();
			_cutoutsLayer = new clt_selectLayer(_toolLayer, cd_masks, clt_servers.MASK_URL, clt_events.SELECTCUTOUT, rect.x, rect.y, rect.width - HEADHEIGHT, rect.height);
			_cutoutsLayer.visible = false;
		}
		
		protected function createCorr(): void {
			var rect: Rectangle = _toolLayer.getInner();
			_corrLayer = new clt_correctionPanel(_toolLayer, rect.x, rect.y, rect.width - HEADHEIGHT, rect.height);
			_corrLayer.visible = false;
		}
		
		public function close(): void {
			doCanClose();
		}
		
		protected function doCanClose(e: Event=null): void {
			var hideRequestObj: Object = {
				result: true
			}
			workspace.dispatchEvent(new ObjectEvent(clt_events.CANHIDETOOLS, hideRequestObj));
			if (hideRequestObj.result) closePanel();
		}
		
		public function closePanel(): void {
			workspace.dispatchEvent(new Event(clt_events.HIDETOOLS));
			hide();
		}
		
		protected function get workspace(): clt_workspace {
			return (app as clt_clothingBase).workspace;
		}
		
		public function get toolLayer(): clt_component {
			return _toolLayer;
		}
		
		protected function showLayer(a_layer: uif_ecomponent): void {
			_toolLayer.forEach(function(elem: uif_ecomponent): void {
				elem.visible = false;
			});
			a_layer.visible = true;
			show();
		}
		
		protected function doShowTools(e: ObjectEvent): void {
			if (e.object as String) {
				switch (e.object as String) {
					case 'cutouts': showLayer(_cutoutsLayer);
									break;
					case 'photos': showLayer(_photosLayer);
									break;
					case 'backgrounds': showLayer(_backgroundLayer);
									break;
					case 'correction': showLayer(_corrLayer);
									break;
				}
			} else showLayer(e.object as uif_ecomponent);
		}
	}
}